package com.bdqn.vueboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.vueboot.entity.SysUser;
import com.bdqn.vueboot.service.SysUserService;
import com.bdqn.vueboot.util.ResultStateEnum;
import io.swagger.annotations.ApiOperation;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Resource
    SysUserService userService;

    @ApiOperation(value = "查询用户列表")
    @CrossOrigin
    @RequestMapping(value = "/list")
    public Object list(@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
                       @RequestParam(value = "usrName", required = false, defaultValue = "") String usrName,
                       @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit,
                       Model model) {
        IPage<SysUser> pageable = new Page<SysUser>(pageIndex ,limit);
        IPage<SysUser> page = userService.findPageByMap(usrName, pageable);
        model.addAttribute("userPager", page);
        model.addAttribute("usrName", usrName);
        return page;
    }

//    @CrossOrigin
//    //@RequestMapping(value = "/add")
//    @ApiOperation(value = "添加用户")
//    @GetMapping(value = "/add")
//    public Object add(
//            @RequestParam(value = "usrName", required = true) String usrName) {
//        SysUser user = new SysUser();
//        user.setUsrName(usrName);
//        try {
//            userService.saveUser(user);
//            return ResultStateEnum.SUCCESS.getResultState();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ResultStateEnum.ERROR.getResultState();
//        }
//    }

    @ApiOperation(value = "根据ID删除")
    @CrossOrigin
    @RequestMapping(value = "/deleteById")
    public Object deleteById(@RequestParam(value = "id", required = true) Long id) {
        try {
            userService.deleteByUserId(id);
            return ResultStateEnum.SUCCESS.getResultState();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultStateEnum.ERROR.getResultState();
        }
    }

//    @ApiOperation(value = "根据ID修改")
//    @CrossOrigin
//    @RequestMapping(value = "/update")
//    public Object update(@RequestParam(value = "id", required = true) Long id,
//                         @RequestParam(value = "usrName", required = true) String usrName) {
//        SysUser user = new SysUser();
//        user.setUsrId(id);
//        user.setUsrName(usrName);
//        try {
//            userService.saveUser(user);
//            return ResultStateEnum.SUCCESS.getResultState();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ResultStateEnum.ERROR.getResultState();
//        }
//    }
}
